﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Linq;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;
using System.Text;
using System.Collections.Specialized;

namespace SVNService
{
    /// <summary>
    /// Web service which can update task informations.
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class UpdateTaskService : System.Web.Services.WebService
    {
        /// <summary>
        /// Updates the specified task.
        /// </summary>
        /// <param name="xml">XML with information to be updated.</param>
        /// <returns>Errors.</returns>
        [WebMethod]
        public string Update(string xml)
        {
            try
            {
                #region [ Parse xml and get information about commited information ]
                XDocument xmlDocument = XDocument.Parse(xml);
                int taskID = Int32.Parse(xmlDocument.Root.Element("taskID").Value);
				string newTaskState = null;
				if (xmlDocument.Root.Element("newTaskState") != null)
				{
					newTaskState = xmlDocument.Root.Element("newTaskState").Value;
				}
                double? workProgress = null;
                double tempWorkProgress;
                if (xmlDocument.Root.Element("workProgress") != null && 
					double.TryParse(xmlDocument.Root.Element("workProgress").Value, out tempWorkProgress))
                {
                    workProgress = tempWorkProgress;
                }
                string revision = xmlDocument.Root.Element("revision").Value;
                string comment = xmlDocument.Root.Element("comment").Value;
                string user = xmlDocument.Root.Element("user").Value;
                #endregion

                #region [ Update task ]
                Task task = DAOTask.GetTaskByID(taskID);

                // If is new task state availible, then update task
                if (!String.IsNullOrEmpty(newTaskState))
                {
                    DAOTask.UpdateTaskState(taskID, newTaskState);
                }

                // if is work progress availible, then update
                if (workProgress != null)
                {
                    DAOWorkProgress.AddProgress(user, taskID, DateTime.Now, workProgress.Value, comment);
                }

				StringBuilder sb = new StringBuilder();
				sb.AppendLine(String.Format("{1} [{0}]", revision, (string)HttpContext.GetLocalResourceObject("~/UpdateTask.asmx", "Revision")));
				if (workProgress.HasValue)
				{
					// Convert hours to storypoints
					User boUser = DAOUser.GetByLogin(user);
					workProgress = DAOUser.HoursToStoryPoints(workProgress.Value, boUser);
					sb.AppendLine(String.Format("{0}h.", workProgress.Value));
				}
				if (!String.IsNullOrEmpty(newTaskState))
				{
					sb.AppendLine(String.Format("{0} {1}", 
						(string)HttpContext.GetLocalResourceObject("~/UpdateTask.asmx", "NewState"), 
						(string)HttpContext.GetLocalResourceObject("~/UpdateTask.asmx", newTaskState)));	
				}
				sb.AppendLine();
				sb.AppendLine(String.Format("{0}", comment));

				DAOComment.CreateComment(user, sb.ToString(), DateTime.Now, task);
                #endregion
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "OK";
        }
    }
}
